#!/bin/bash

host_1_ip_0=192.168.2.73
host_1_ip_1=192.168.11.73

host_2_ip_0=192.168.2.74
host_2_ip_1=192.168.11.74

setenforce 0
systemctl stop firewalld
systemctl disable firewalld
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config


############################# mysql_1 配置文件(master) #####################################
cp /etc/my.cnf{,.bak}
cat <<EOF    >/etc/my.cnf
[client]
port = 3306
socket = /data/mysql/data/mysql.sock

[mysqld]
port = 3306
user = mysql
server-id = $(ip address | grep inet | grep eth0$ | awk -F '.' '{print $4}' | awk -F '/' '{print $1}')
bind-address = 0.0.0.0
socket = /data/mysql/data/mysql.sock

basedir = /usr/local/mysql
datadir = /data/mysql/data
character-set-server = utf8
pid-file = /data/mysql/data/mysql.pid

back_log = 300
skip-name-resolve

max_connections = 1000
max_connect_errors = 6000
open_files_limit = 65535
table_open_cache = 128
max_allowed_packet = 500M
binlog_cache_size = 1M
max_heap_table_size = 8M
tmp_table_size = 16M

read_buffer_size = 2M
read_rnd_buffer_size = 8M
sort_buffer_size = 8M
join_buffer_size = 8M
key_buffer_size = 4M

thread_cache_size = 8

query_cache_type = 1
query_cache_size = 8M
query_cache_limit = 2M

ft_min_word_len = 4

log_bin = mysql-bin
# binlog_format = mixed
binlog_format = row
expire_logs_days = 7

slow_query_log = 1
long_query_time = 1
log_error = /data/mysql/logs/error.log
slow_query_log_file = /data/mysql/logs/mysql-slow.log

performance_schema = 0
explicit_defaults_for_timestamp

#lower_case_table_names = 1

skip-external-locking

#default_storage_engine = InnoDB
#default-storage-engine = MyISAM
innodb_file_per_table = 1
innodb_open_files = 500
innodb_buffer_pool_size = 5000M
innodb_write_io_threads = 4
innodb_read_io_threads = 4
innodb_thread_concurrency = 0
innodb_purge_threads = 1
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 8M
innodb_log_file_size = 128M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120

bulk_insert_buffer_size = 64M
myisam_sort_buffer_size = 32M
myisam_max_sort_file_size = 10M
myisam_repair_threads = 1

interactive_timeout = 28800
wait_timeout = 28800

# relay_log = relay-bin
# relay_log_index = relay-bin
# relay_log = /data/mysql/logs/relay-log.log


[mysqldump]
quick
max_allowed_packet = 500M

[myisamchk]
key_buffer_size = 8M
sort_buffer_size = 8M
read_buffer = 4M
write_buffer = 4M
EOF



############################# mysql_2 配置文件(slave) #####################################
cp /etc/my.cnf{,.bak}
cat <<EOF    >/etc/my.cnf
[client]
port = 3306
socket = /data/mysql/data/mysql.sock

[mysqld]
port = 3306
user = mysql
server-id = $(ip address | grep inet | grep eth0$ | awk -F '.' '{print $4}' | awk -F '/' '{print $1}')
bind-address = 0.0.0.0
socket = /data/mysql/data/mysql.sock

basedir = /usr/local/mysql
datadir = /data/mysql/data
character-set-server = utf8
pid-file = /data/mysql/data/mysql.pid

back_log = 300
skip-name-resolve

max_connections = 1000
max_connect_errors = 6000
open_files_limit = 65535
table_open_cache = 128
max_allowed_packet = 500M
binlog_cache_size = 1M
max_heap_table_size = 8M
tmp_table_size = 16M

read_buffer_size = 2M
read_rnd_buffer_size = 8M
sort_buffer_size = 8M
join_buffer_size = 8M
key_buffer_size = 4M

thread_cache_size = 8

query_cache_type = 1
query_cache_size = 8M
query_cache_limit = 2M

ft_min_word_len = 4

# log_bin = mysql-bin
# binlog_format = mixed
# binlog_format = row
# expire_logs_days = 7

slow_query_log = 1
long_query_time = 1
log_error = /data/mysql/logs/error.log
slow_query_log_file = /data/mysql/logs/mysql-slow.log

performance_schema = 0
explicit_defaults_for_timestamp

#lower_case_table_names = 1

skip-external-locking

#default_storage_engine = InnoDB
#default-storage-engine = MyISAM
innodb_file_per_table = 1
innodb_open_files = 500
innodb_buffer_pool_size = 5000M
innodb_write_io_threads = 4
innodb_read_io_threads = 4
innodb_thread_concurrency = 0
innodb_purge_threads = 1
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 8M
innodb_log_file_size = 128M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120

bulk_insert_buffer_size = 64M
myisam_sort_buffer_size = 32M
myisam_max_sort_file_size = 10M
myisam_repair_threads = 1

interactive_timeout = 28800
wait_timeout = 28800

relay_log = relay-bin
relay_log_index = relay-bin
relay_log = /data/mysql/logs/relay-log.log


[mysqldump]
quick
max_allowed_packet = 500M

[myisamchk]
key_buffer_size = 8M
sort_buffer_size = 8M
read_buffer = 4M
write_buffer = 4M
EOF

# 安装依赖环境
yum install -y vim gcc gcc-c++ wget autoconf net-tools lrzsz lsof iotop bash-completion
yum install -y curl policycoreutils openssh-server openssh-clients postfix

# 准备安装环境
useradd mysql -s /sbin/nologin
mkdir -pv /data/mysql/{data,logs}
touch /data/mysql/logs/error.log
chown -R mysql:mysql /data

# 下载解压安装
wget http://mirrors.ustc.edu.cn/mysql-ftp/Downloads/MySQL-5.7/mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz
tar xvf mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz -C /usr/local/src
ln -sv /usr/local/src/mysql-5.7.23-linux-glibc2.12-x86_64 /usr/local/mysql
chown -R mysql:mysql /usr/local/mysql/*

# 初始化
/usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --datadir=/data/mysql/data --basedir=/usr/local/mysql
cp /usr/local/src/mysql-5.7.23-linux-glibc2.12-x86_64/support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
ln -sv /usr/local/mysql/bin/mysql /usr/bin/mysql
chkconfig --add mysqld
chkconfig mysqld on

systemctl start mysqld
# /etc/init.d/mysqld start

# 初始化(删除默认账号及测试数据库,设置root密码)
/usr/local/mysql/bin/mysql_secure_installation



############################# mysql_1 创建同步配置 #####################################
# 授权tom从192.168.2.*使用密码123456同步数据
mysql -uroot -p12345678 -e "
grant replication slave,replication client on *.* to 'tom'@'192.168.2.%' identified by 'ZAQ123.com';
flush privileges;
show master status;"


############################# mysql_2 配置同步 #####################################
mysql -uroot -p12345678 -e "
change master to master_host='192.168.2.73',  # mysql_1数据库 IP或主机名(需解析通)
master_port=3306,                             # mysql_1数据库端口号
master_user='tom',                            # 同步账号
master_password='ZAQ123.com',                 # 同步账号的密码
master_log_file='mysql-bin.000002',           # mysql_1数据库执行 show master status; 获取的 File 值
master_log_pos=1174;                          # mysql_1数据库执行 show master status; 获取的 Position 值
start slave;                                  # 启动slave
show slave status\G;"                         # 查看同步状态